/****************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "DevDatabinTask.hpp"
#include "../../FileOperations/fileops.h"
#include "../../Saves/DataBin/WiiSaveCrypted.h"
#include "../../Saves/SaveTools.h"
#include "../../Menu/Explorer.h"

DevDatabinTask::DevDatabinTask(short Type)
	: type(Type)
	, canceled(false)
	, window(NULL)
{
	ThreadedTaskHandler::Instance()->AddTask(this);
}

void DevDatabinTask::OnCancelClick()
{
	canceled = true;
}

void DevDatabinTask::Execute(void)
{
	if(type == INSTALL_DATABIN_TO_NAND || type == INSTALL_DATABIN_TO_ENAND)
	{
		window = new ManageProgressWindow(tr("Installing..."));
		window->SetCancel(false);
	}
	else if(type == DELETE_DATABIN_FROM_DEV)
		window = new ManageProgressWindow(tr("Deleting..."));
	else if(type == DECOMPRESS_DATABIN_TO_DEV)
		window = new ManageProgressWindow(tr("Decompressing..."));
	else if(type == COPY_DATABIN_TO_DEV)
		window = new ManageProgressWindow(tr("Copying..."));
	
	if(type != INSTALL_DATABIN_TO_NAND && type != INSTALL_DATABIN_TO_ENAND)
		window->Canceled.connect(this, &DevDatabinTask::OnCancelClick);
	
	int result = -1;
	if(type == DELETE_DATABIN_FROM_DEV)
	{
		if(RemoveFile(CurrentBrowser()->GetCurrentSelectedFilepath()) > 0)
			result = 1;
	}
	else if(type == DECOMPRESS_DATABIN_TO_DEV)
	{
		WiiSaveCrypted * saveCrypted = new WiiSaveCrypted();
		if(saveCrypted->Decompress(CurrentBrowser()->GetCurrentSelectedFilepath(), Settings.TmpPath))
			result = 1;
		
		delete saveCrypted;
	}
	else if(type == COPY_DATABIN_TO_DEV)
	{
		std::string srcPath = CurrentBrowser()->GetCurrentSelectedFilepath();
		u64 tid = 0;
		WiiSaveCrypted::GetBannerAndTid(srcPath, &tid);
		std::string ID = SaveTools::titleText(TITLE_UPPER(tid), TITLE_LOWER(tid));
		
		std::string destPath = Settings.TmpPath;
		if(destPath[destPath.size()-1] != '/')
			destPath += "/";
		destPath += ID;
		CreateSubfolder(destPath);
		destPath += "/data.bin";
		
		if(CopyFile(srcPath.c_str(), destPath.c_str()) > 0)
			result = 1;
	}
	else if(type == INSTALL_DATABIN_TO_ENAND)
	{
		WiiSaveCrypted * saveCrypted = new WiiSaveCrypted();
		if(saveCrypted->EmuImport(CurrentBrowser()->GetCurrentSelectedFilepath(), Settings.TmpPath))
			result = 1;
		
		delete saveCrypted;
	}
	else if(type == INSTALL_DATABIN_TO_NAND)
	{
		WiiSaveCrypted * saveCrypted = new WiiSaveCrypted();
		if(saveCrypted->Import(CurrentBrowser()->GetCurrentSelectedFilepath()))
			result = 1;
		
		delete saveCrypted;
	}
	
	delete window;
	
	if(canceled)
		result = 0;
	
	End(this, result);
}
